package com.mentalhealthplatform.studentV3.controller;

import com.mentalhealthplatform.appModels.Test;
import com.mentalhealthplatform.studentV3.service.TestServiceV3;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author Pear
 */
@RestController
@RequestMapping("api/v3/tests")
public class TestControllerV3 {

    @Autowired
    private TestServiceV3 testServiceV3;

    @GetMapping
    public List<Test> getAllTests() {
        return testServiceV3.findAll();
    }

    @GetMapping("/{id}")
    public Test getTestById(@PathVariable Integer id) {
        return testServiceV3.findById(id);
    }

    @GetMapping("/random")
    public ResponseEntity<List<Test>> getRandomQuestions(@RequestParam int count, @RequestParam(required = false) Integer type) {
        List<Test> tests = testServiceV3.getRandomTestsAndSetStatus(count, type);
        return ResponseEntity.ok(tests);
    }


    @PostMapping
    public Test createTest(@RequestBody Test test) {
        return testServiceV3.save(test);
    }

    @DeleteMapping("/{id}")
    public void deleteTest(@PathVariable Integer id) {
        testServiceV3.deleteById(id);
    }

    @PostMapping("/update-status")
    public ResponseEntity<Void> updateTestStatus(@RequestBody Map<String, Object> payload) {
        try {
            List<Integer> questionIds = (List<Integer>) payload.get("questionIds");
            Integer status = (Integer) payload.get("status");

            testServiceV3.updateTestStatus(questionIds, status);
            return ResponseEntity.ok().build();
        } catch (Exception e) {
            e.printStackTrace(); // 打印异常堆栈信息，用于调试
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }

    // 其他可能的端点
}

